<?php
/**
 * Created by project.
 * User: huanzipap
 * Date: 2023/6/8
 * Time: 9:37
 */

namespace App\Controller;

use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Hyperf\WebSocketServer\Constant\Opcode;
use Swoole\Server;
use Swoole\WebSocket\Server as WebSocketServer;

class WebSocketController implements OnMessageInterface,OnOpenInterface,OnCloseInterface
{

    /**
     * @inheritDoc
     */
    public function onMessage($server, $frame): void
    {
        var_dump($frame);
        $data = [
            "hello!",
            "hello，叼毛",
            "叼您的啦",
            "做点事，叼毛",
            "中午了，要吃饭了",
            "好烦啊",
            "上班上班",
            "寄",
            "该喝水了，记得喝水，叼毛",
            "还要上班，淦",
            "中午吃啥",
        ];
        $rand = rand(0,10);
        $server->push($frame->fd, 'huanzipap: ' . $data[$rand]);
    }


    public function onClose($server, int $fd, int $reactorId): void
    {
        echo $fd . '断开连接,处理线程是 ' . $reactorId . PHP_EOL;
    }

    public function onOpen($server, $request): void
    {
        $server->push($request->fd, $request->fd . ' 号客户端已连接!' . PHP_EOL);
    }

}
